<?php

namespace app\index\service;

/**
 * Description of Api
 * @author Dongasai
 */
class Api
{

    /**
     * 编辑数据
     * @param type $uid
     * @param type $data
     */
    public static function edit($uid, $data)
    {
        $ApiModel = \think\Loader::model('api');
        $re       = $ApiModel->save($data, ['user_id' => $uid]);
        if ($re) {
            return true;
        } else {
            return "保存数据库出错!";
        }
    }

    public static function info($id)
    {
        $ApiModel = \think\Loader::model('api');
        return $ApiModel->get(['id' => $id]);
    }

    /**
     * 增加线路
     * @param type $uid
     * @param array $data
     * @return boolean|string
     */
    public static function add($uid, $data)
    {
        $data['user_id'] = $uid;
        $ApiModel        = \think\Loader::model('api');
        $re              = $ApiModel->data($data)->save();
        if ($re) {
            return true;
        } else {
            return "保存数据库出错!!";
        }
    }

    /**
     * 分页获取Api列表
     * @param type $uid 用户
     * @param type $cate_id 分类
     * @param type $order 排序
     * @param type $page 当前页数
     * @param type $low 分页条数
     */
    public static function listForPage($uid, $cate_id, $order = '', $page = 1, $low = 10)
    {
        $ApiModel = \think\Loader::model('api');
        $where    = [
            'cate_id' => $cate_id,
            'user_id' => $uid
        ];
        $totle    = $ApiModel->where($where)->count();
        $list     = $ApiModel->where($where)->order($order)->page($page, $low)->select();
        return ['totle' => $totle, 'list' => $list];
    }

}
